package com.web.test.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/*
* 在springMVC中，拦截器主要用于拦截Handler（controller）方法
* */
public class CheckUserInterceptor implements HandlerInterceptor {

    /*
    * 在进入访问的handler之前拦截
    * true：放行
    * false：不放行
    * */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session=request.getSession();
        //用户未登录，则返回登录页面；已登录则放行
        if(session.getAttribute("user")!=null){
            return true;
        }else{
            response.sendRedirect("/login.html");
            return  false;
        }
    }
}
